package busmap.main;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
import busmap.database.DatabaseAdapter;


import com.google.android.maps.GeoPoint;

public class ListBusStop extends ListActivity {
	private DatabaseAdapter mDbHelper ;
	private ListView lv;
	public static GeoPoint  dest_GeoPoint;
    private double src_latitude;
    private double src_longitude;
	private LocationManager locationManager;
	private LocationListener locationListener;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  mDbHelper = new DatabaseAdapter(this);
      mDbHelper.open();
      //mDbHelper.createNote("224 Tô hiến thành", "10.7806554", "106.6691139");
      //mDbHelper.createNote("250 Cách mạng tháng 8", "10.7746005", "106.6874683");
    //  mDbHelper.deleteNote(0);
      lv = getListView();
	  lv.setTextFilterEnabled(true);	 
	  lv.setOnItemClickListener(new OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent, View view,  int position, long id) {	  
	    	dest_GeoPoint=  mDbHelper.getGeoPoint(Integer.valueOf(String.valueOf((lv.getItemIdAtPosition(position))))); 
	    	if(dest_GeoPoint != null){
	    		BusMapActivity.tabHost.setCurrentTab(2);
	    	}
	    }
	  });	  
	  	locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
		locationListener = new GPSLocationListener();
	    locationManager.requestLocationUpdates(  LocationManager.GPS_PROVIDER, 0, 0, locationListener);
	}
	
	
	private void fillData() {
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        RowAdapter adapter = new RowAdapter(this,notesCursor);
        setListAdapter(adapter);
    }

	 
	class RowAdapter extends CursorAdapter {
		private Cursor mCursor;  
	    private Context mContext;  
	    private final LayoutInflater mInflater;
		public RowAdapter(Context context, Cursor c) {
			super(context, c);
			  mInflater = LayoutInflater.from(context);  
		      mContext = context; 
		      mCursor = c;
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public void bindView(View view, Context conetxt, Cursor cursor) {
			TextView address = (TextView) view.findViewById(R.id.address);
			TextView distant = (TextView) view.findViewById(R.id.distant);
			View listid =  view.findViewById(R.layout.list_item);	
			if(address != null)
				address.setText(cursor.getString(1));
			if(distant != null){
				double latitude   =  Double.parseDouble(cursor.getString(2));
				double longtitude = Double.parseDouble(cursor.getString(3));
				double dis = CalculationByDistance(src_latitude, src_longitude, latitude, longtitude);
	    		distant.setText(makeDisString(dis));
			}
			if(listid != null)
				listid.setId(cursor.getInt(0));			
		}
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			 final View view = mInflater.inflate(R.layout.list_item, parent, false);  
		     return view;  
		}  		
	}
   private String makeDisString(double dis){
	   if(dis < 1000) return String.valueOf(Math.round(dis)).concat(" m");
	   else return String.valueOf(Math.round(dis/100)*1.0/10).concat(" km");
   }
   private double CalculationByDistance(double lat1, double lon1, double lat2, double lon2) {  
	     double dLat = Math.toRadians(lat2-lat1);  
	      double dLon = Math.toRadians(lon2-lon1);  
	      double a = Math.sin(dLat/2) * Math.sin(dLat/2) +  
	         Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *  
	         Math.sin(dLon/2) * Math.sin(dLon/2);  
	      double c = 2 * Math.asin(Math.sqrt(a));  
	      return 6371000 * c;  
	   }  
	   
	   
	private class GPSLocationListener implements LocationListener {
		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			if(location != null){
				 src_latitude  = 	location.getLatitude();
				 src_longitude = 	location.getLongitude();				
				 fillData();
			}
		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
	}
}
